﻿/*************************************************************************/
/** Copyright.                                                          **/
/** FileName: MeasureB.c                                                **/
/** Author: kongls                                                      **/
/** Version: 1.0.0.0                                                    **/
/** Description: measure B for leq withou headfile                      **/
/*************************************************************************/
//=======================================================================//
//= Include files.                                                      =//
//=======================================================================//
#include "DemoProc.h"
#include "HMI_Engine.h"
#include "SGUI_Basic.h"

extern char time_str[64];
extern LEQ_DATA_EVENT_DATA  LeqData;
extern char battery_str[3];
//=======================================================================//
//= Macro definition.                                                   =//
//=======================================================================//

//=======================================================================//
//= Static function declaration.                                        =//
//=======================================================================//
static HMI_ENGINE_RESULT    HMI_MeasureB_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT    HMI_MeasureB_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT    HMI_MeasureB_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID);
static HMI_ENGINE_RESULT    HMI_MeasureB_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID);

//=======================================================================//
//= Static variable declaration.                                        =//
//=======================================================================//
HMI_SCREEN_ACTION       s_stMeasureBPaintActions =     {   NULL,
                                                            HMI_MeasureB_Prepare,
                                                            HMI_MeasureB_RefreshScreen,
                                                            HMI_MeasureB_ProcessEvent,
                                                            HMI_MeasureB_PostProcess
                                                        };
//=======================================================================//
//= Global variable declaration.                                        =//
//=======================================================================//
HMI_SCREEN_OBJECT       g_stHMIMeasureBPaint =        {   HMI_SCREEN_ID_MeasureB_PAINT,
                                                            &s_stMeasureBPaintActions
                                                        };

//=======================================================================//
//= Function define.                                                    =//
//=======================================================================//
HMI_ENGINE_RESULT HMI_MeasureB_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
    SGUI_Basic_ResetMask(pstDeviceIF);
    if(NULL != pstDeviceIF->fnClear)
    {
        pstDeviceIF->fnClear();
    }
    else
    {
        SGUI_Basic_DrawRectangle1(pstDeviceIF, 0, 0, pstDeviceIF->stSize.iWidth, pstDeviceIF->stSize.iHeight, SGUI_COLOR_BKGCLR, SGUI_COLOR_BKGCLR);
    }
    SGUI_Basic_ResetMask(pstDeviceIF);
    HMI_MeasureB_RefreshScreen(pstDeviceIF, pstParameters);

    printf("HMI_MeasureB_Prepare\n");
    return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_MeasureB_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
    printf("2 HMI_MeasureB_RefreshScreen\n");
    /*----------------------------------*/
    /* Variable Declaration             */
    /*----------------------------------*/
    SGUI_INT                iPaintBkgIdx;

    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
   
    // Paint background.  20251104  切界面时会造成绘制白色线条  各个子界面都注释掉
    // for(iPaintBkgIdx=0; iPaintBkgIdx<pstDeviceIF->stSize.iHeight; iPaintBkgIdx+=5)
    // {
    //     SGUI_Basic_DrawHorizontalLine(pstDeviceIF, 0, pstDeviceIF->stSize.iWidth-1, iPaintBkgIdx, SGUI_COLOR_FRGCLR);
    // }
    return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_MeasureB_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID)
{
    if(NULL != pstDeviceIF->fnClear)
    {
        pstDeviceIF->fnClear();
    }

    // printf("3 Basic_ProcessEvent id=%d\n", *piActionID);
    /*----------------------------------*/
    /* Variable Declaration             */
    /*----------------------------------*/
    HMI_ENGINE_RESULT           eProcessResult;
    SGUI_INT                    iProcessAction;
    KEY_PRESS_EVENT*            pstKeyEvent;
    // LEQ_EVENT*            pstKeyEvent;
    char cLeq[12];

    /*----------------------------------*/
    /* Initialize                       */
    /*----------------------------------*/
    eProcessResult =            HMI_RET_NORMAL;
    iProcessAction =            HMI_DEMO_PROC_NO_ACT;
    pstKeyEvent =               (KEY_PRESS_EVENT*)pstEvent; // (KEY_PRESS_EVENT*)pstEvent;
  
    if(EVENT_ID_KEY_PRESS == pstEvent->iID)
    {
        printf("%s:%d EVENT_ID_KEY_PRESS keyvalue=%d\n", __FILE__, __LINE__, pstKeyEvent->Data.uiKeyValue);

        switch(pstKeyEvent->Data.uiKeyValue)
        {
            case KEY_VALUE_HOME:
            {
                // Event data is invalid.
                eProcessResult = HMI_RET_INVALID_DATA;
                break;
            }
            case KEY_VALUE_MENU:
            {
                // go to main menu
                printf("menu\n");
                //iProcessAction = HMI_DEMO_PROC_CANCEL;
                // 20251104第二个测量界面按MENU跨界面回到菜单 use :HMI_SwitchToScreen,  
                // 问题：切换界面时有判断，需要注释掉，可能带来的问题 pstPrevious为空
                    // if(NULL != pstDestScreen->pstPrevious)
                    // {
                    //     /* Cannot reenter to a screen object. */
                    //     eProcessResult = HMI_RET_ERROR;
                    // }
                iProcessAction = HMI_SwitchToScreen(HMI_SCREEN_ID_DEMO_LIST, NULL);
                return ;
                break;
            }
            case KEY_VALUE_PAUSE:
            {
                // hold / unhold
                LeqData.uHoldStatus = !LeqData.uHoldStatus;
                printf("hold/unhold\n");
                break;
            }
            case KEY_VALUE_ENTER:
            {
                // FS
                LeqData.uTimeWeightType = !LeqData.uTimeWeightType;
                printf("Fast/Slow\n");
                break;
            }
            case KEY_VALUE_LEFT:
            case KEY_VALUE_RIGHT:
            {
                printf("to first screen\n");
                // go to first screen
                iProcessAction = HMI_DEMO_PROC_CANCEL;
                break;
            }
            default:
            {
                break;
            }
        }
    }
    
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, time_str, &SGUI_DEFAULT_FONT_16, 1, 1, 80, SGUI_DRAW_NORMAL);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, battery_str, &SGUI_DEFAULT_FONT_16, 91, 1, 160, SGUI_DRAW_NORMAL);

    snprintf(cLeq,sizeof(cLeq), "%.1f dB", (float)LeqData.uLeqA / 10);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "SPL A", &SGUI_DEFAULT_FONT_16, 1,  25, 50, SGUI_DRAW_NORMAL);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, cLeq, &SGUI_DEFAULT_FONT_16, 30, 40, 160, SGUI_DRAW_NORMAL);

    snprintf(cLeq,sizeof(cLeq), "%.1f dB", (float)LeqData.uLeqC / 10);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "SPL C", &SGUI_DEFAULT_FONT_16, 1,  63, 50, SGUI_DRAW_NORMAL);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, cLeq, &SGUI_DEFAULT_FONT_16, 30, 78, 160, SGUI_DRAW_NORMAL);
 
    snprintf(cLeq,sizeof(cLeq), "%.1f dB", (float)LeqData.uLeqZ / 10);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "SPL Z", &SGUI_DEFAULT_FONT_16, 1,  101, 50, SGUI_DRAW_NORMAL);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, cLeq, &SGUI_DEFAULT_FONT_16, 30, 116, 160, SGUI_DRAW_NORMAL);
 

    // Fast/Slow
    if(0 == LeqData.uTimeWeightType)
    {
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Slow", &SGUI_DEFAULT_FONT_16, 80,  25, 120, SGUI_DRAW_NORMAL);
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Slow", &SGUI_DEFAULT_FONT_16, 80, 60, 120, SGUI_DRAW_NORMAL);
    }
    else
    {
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Fast", &SGUI_DEFAULT_FONT_16, 80,  25, 120, SGUI_DRAW_NORMAL);
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Fast", &SGUI_DEFAULT_FONT_16, 80, 60, 120, SGUI_DRAW_NORMAL);
    }

    // current H FS status
    if(0 == LeqData.uHoldStatus)  // unhold
    {
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "", &SGUI_DEFAULT_FONT_16, 1, 135, 40, SGUI_DRAW_NORMAL);
    }
    else  // hold
    {
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Hold", &SGUI_DEFAULT_FONT_16, 1, 135, 40, SGUI_DRAW_NORMAL);
    }

    // over/low
    if(LeqData.uLeqA > 900)
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "over", &SGUI_DEFAULT_FONT_16, 40, 135, 127, SGUI_DRAW_NORMAL);
    else if(LeqData.uLeqA < 400)
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "low", &SGUI_DEFAULT_FONT_16, 40, 135, 127, SGUI_DRAW_NORMAL);
    else
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "", &SGUI_DEFAULT_FONT_16, 40, 135, 127, SGUI_DRAW_NORMAL);

    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/

    if(NULL != piActionID)
    {
        *piActionID = iProcessAction;
    }
    return eProcessResult;
}

HMI_ENGINE_RESULT HMI_MeasureB_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID)
{
    //printf("4 HMI_MeasureB_PostProcess id=%d\n\n", iActionID);
    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/

    //printf(" basic  post process  id=%d\n", iActionID);
    if(HMI_PROCESS_SUCCESSFUL(eProcResult))
    {
        if(HMI_DEMO_PROC_CANCEL == iActionID)
        {
            HMI_GoBack(NULL);
        }
    }

    return HMI_RET_NORMAL;
}



